package com.crawler.utils

import java.security.MessageDigest

import com.crawler.message.TaskParamMessage

/**
  * Created by tongtao.zhu on 2017/1/5.
  */
object MD5Utils {
  def getMd5String(task: TaskParamMessage): String = {
    val md  = MessageDigest.getInstance("MD5")
    md.update((task.host+task.data+task.param).getBytes())
    val hash = md.digest()
    val buffer = new StringBuffer("")
    hash.map(item =>  {
      var i = item.toInt
      if ( i < 0){
        i =  i + 256
      }
      if ( i < 16){
        buffer.append("0")
      }
      buffer.append(Integer.toHexString(i))
    })
    buffer.toString.substring(8, 24)
  }

  def getMd5String(task: String): String = {
    val md  = MessageDigest.getInstance("MD5")
    md.update(task.getBytes())
    val hash = md.digest()
    val buffer = new StringBuffer("")
    hash.map(item =>  {
      var i = item.toInt
      if ( i < 0){
        i =  i + 256
      }
      if ( i < 16){
        buffer.append("0")
      }
      buffer.append(Integer.toHexString(i))
    })
    buffer.toString.substring(8, 24)
  }
}
